<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="top_header_left">
      </div>
      <div class="top_header_right">
        <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px"
          @click="simpleGiftList();dialogFormVisible1 = true;">
          新增
        </el-button>
      </div>
    </div>

    <el-table ref="multipleTable" :data="list" stripe style="width: 1200px" border>
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column prop="icon" label="礼物名称" align="center">
        <template align="center" slot-scope="scope">
          <div class="demo-image__preview">
            <el-image class="my-img" :src="scope.row.icon" ref="myImg" :preview-src-list="srcList"
              @click="handlePriveImg(scope.row.icon)" @click.stop="handleClickItem">
            </el-image>
            <span>{{scope.row.name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="money" label="礼物价值" align="center" />
      <el-table-column prop="all_num" label="数量" align="center" />
      <el-table-column prop="is_show" label="是否显示" align="center">
        <template align="center" slot-scope="scope">
          <span v-if="scope.row.is_show == '1'">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template align="center" slot-scope="scope">
          <el-button type="button" size="small"
            @click="simpleGiftList();add_match = {...scope.row};dialogFormVisible1 = true;">
            编辑
          </el-button>
          <el-button type="button" size="small" @click="deletGiftList(scope.row);">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="add_match.id?'编辑':'新增'" :visible.sync="dialogFormVisible1" style="width:800px;margin:0 auto;"
      class="model_">
      <div class="center_lisr">
        <div>
          <span>选择礼物</span>
          <el-select v-model="add_match.gift_id" placeholder="选择礼物" clearable class="filter-item" filterable
            style="width: 300px;max-width:70%;">
            <el-option v-for="item in calendarTypeOptions1" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </div>
        <div>
          <span>数量</span>
          <el-input v-model="add_match.all_num" placeholder="数量" style="width:300px;max-width:70%;" type="number" />
        </div>
        <div>
          <span>是否显示</span>
          <div style="width:300px;max-width:70%;display:flex;">
            <el-radio-group v-model="add_match.is_show">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div>
          <el-button class="filter-item" type="primary" style="width:100%;margin:0 auto;margin-top:10px"
            @click="baocun">
            保存
          </el-button>
        </div>
      </div>

    </el-dialog>



  </div>
</template>
<script>
  import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
  import {
    encryption2
  } from "@/api/index";
  export default {
    name: "ComplexTable",
    components: {
      Pagination,
    },
    data() {
      return {
        calendarTypeOptions1: [],
        list: [],
        time_list: {},
        add_match: {},
        goodList: {}, //编辑列表
        dialogFormVisible1: false,
        srcList: [],
      };
    },
    created() {
      console.log(this.$route.params.row);
      this.add_match.type = this.$route.params.row.id
      this.handleFilter();
    },
    watch: {
      dialogFormVisible1: {
        handler(newName, oldName) {
          if (!newName) {
            this.add_match.gift_id = '';
            this.add_match.all_num = '';
            this.add_match.is_show = '';
            // this.add_match.cid = this.$route.params.row.id
          }
        },
        deep: true
      }
    },
    methods: {
      baocun() {
        if (this.add_match.id) {
          this.editgift()
        } else {
          this.addgift()
        }
      },
      deletGiftList(item) {
        // console.log(item.id);
        const arys = {
          id: item.id,
        };
        const url = "/gift/giftSubDelete";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.handleFilter();
            this.$message({
              message: res.data.text,
              type: "success",
            });
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      addgift() {
        const arys = {
          type: this.add_match.type,
          gift_id: this.add_match.gift_id,
          all_num: this.add_match.all_num,
          is_show: this.add_match.is_show,
        };
        const url = "/gift/giftSubAdd";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.handleFilter();
            this.$message({
              message: res.data.text,
              type: "success",
            });



            this.dialogFormVisible1 = false;
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      editgift() {
        const arys = {
          id: this.add_match.id,
          gift_id: this.add_match.gift_id,
          all_num: this.add_match.all_num,
          is_show: this.add_match.is_show,
        };
        const url = "/gift/giftSubEdit";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.handleFilter();
            this.$message({
              message: res.data.text,
              type: "success",
            });
            this.dialogFormVisible1 = false;
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      simpleGiftList() {
        const arys = {};
        const url = "/gift/simpleGiftList";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.calendarTypeOptions1 = res.data.result;
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      handlePriveImg(row) {
        this.srcList = []
        this.srcList.push(row); //将后端的这一行的数据push进数组中
        this.$refs.myImg.clickHandler();
      },
      // 获取图片遮罩层dom
      handleClickItem() {
        setTimeout(function () {
          let domImageMask = document.querySelector(".el-image-viewer__wrapper");
          if (!domImageMask) {
            return;
          }
          domImageMask.addEventListener("click", (e) => {
            if (e.target.parentNode.className == 'el-image-viewer__actions__inner' || e.target.parentNode
              .className == 'el-image-viewer__btn el-image-viewer__next' || e.target.parentNode.className ==
              'el-image-viewer__btn el-image-viewer__prev') {
              return; //如果点击底部菜单，不关闭
            }
            // 点击遮罩层时调用关闭按钮的 click 事件
            document.querySelector(".el-image-viewer__close").click();
            document.querySelector('body').setAttribute('style', 'overflow:auto;')
          });
        }, 300)
      },


      // 查询
      handleFilter() {
        const arys = {
          type: this.add_match.type
        };
        const url = "/gift/giftSubList";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.list = res.data.result.list;
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
    },
  };
</script>
<style scoped>
  .filter-container .filter-item {
    margin-bottom: 0;
  }

  .el-table--border {
    border-right: 1px solid #dfe6ec;
  }

  .el-alert {
    width: 1200px;
  }

  .filter-container {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 1200px;
  }

  .batch {
    width: 1200px;
    margin: 10px 0;
    display: flex;
    flex-direction: row-reverse;
  }

  .batch .filter-item {
    margin-left: 20px;
  }

  .el-dialog {
    width: 300px;
    height: 300px;
  }

  .center_lisr {
    width: 96%;
    height: 80%;
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: center;


  }

  .flex_de>div {
    display: flex;
    flex-direction: column;
  }

  .center_lisr>div {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;
  }

  .center_lisr>div>el-input {
    flex: 1;
    margin-left: 20px;
  }

  .model_ /deep/ .el-dialog__body {
    padding-top: 0%;
  }

  .model_ /deep/ .el-button--mini {
    padding: 10px 20px;
  }

  .model_ /deep/ .el-dialog {
    border-radius: 4px;
  }

  /* .model_ /deep/ .el-input__inner {
    text-align: center;
  } */

  .flex_de /deep/ .el-input__inner {
    padding: 0 4px;
    border: 0;
    background: 00000000;
  }

  .new_row {
    width: 80%;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #333;
    font-weight: blod;
    font-family: serif;
    cursor: pointer;
  }

  .my-img {
    width: 80px;
    /* height: 80px; */
    padding: 10px;
    margin: 0 auto;
  }
  /* .my-img /deep/ .el-image__inner{
    width: fit-content;
  } */

  .demo-image__preview {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
  }
</style>